package com.ruoyi.web.controller.note;

import java.util.List;

import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.note.domain.TBook;
import com.ruoyi.note.service.ITBookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 笔记管理Controller
 *
 * @author cl
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/note/book")
public class TBookController extends BaseController {
    @Autowired
    private ITBookService tBookService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询笔记管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBook tBook, HttpServletRequest request) {
        tBook.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        startPage();
        List<TBook> list = tBookService.selectTBookList(tBook);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('note:book:list')")
    @GetMapping("/getAll")
    public TableDataInfo getAll(TBook tBook, HttpServletRequest request) {
        tBook.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        List<TBook> list = tBookService.selectTBookList(tBook);
        return getDataTable(list);
    }

    /**
     * 导出笔记管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:book:export')")
    @Log(title = "笔记管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBook tBook, HttpServletRequest request) {
        tBook.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        List<TBook> list = tBookService.selectTBookList(tBook);
        ExcelUtil<TBook> util = new ExcelUtil<TBook>(TBook.class);
        return util.exportExcel(list, "笔记管理数据");
    }

    /**
     * 获取笔记管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tBookService.selectTBookById(id));
    }

    /**
     * 新增笔记管理
     */
    @PreAuthorize("@ss.hasPermi('note:book:add')")
    @Log(title = "笔记管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBook tBook, HttpServletRequest request) {
        tBook.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        return toAjax(tBookService.insertTBook(tBook));
    }

    /**
     * 修改笔记管理
     */
    @PreAuthorize("@ss.hasPermi('note:book:edit')")
    @Log(title = "笔记管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBook tBook, HttpServletRequest request) {
        tBook.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        return toAjax(tBookService.updateTBook(tBook));
    }

    /**
     * 删除笔记管理
     */
    @PreAuthorize("@ss.hasPermi('note:book:remove')")
    @Log(title = "笔记管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBookService.deleteTBookByIds(ids));
    }
}
